/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package org.luxor.generator.utils;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.rtf.RtfWriter2;

import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 文档生成器 工具类
 *
 * @author Mr.Yan  @date: 2022/5/21
 */
public class GenWordUtils {

    /**
     * 生成文档
     */
    public static Document createWord(OutputStream out) throws DocumentException {
        // 创建word文档,并设置纸张的大小
        Document document = new Document();
        RtfWriter2.getInstance(document, out);
        document.open();
        // 设置文档标题
        Paragraph title = new Paragraph("数据库表设计文档", new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
        title.setAlignment(1);
        document.add(title);
        return document;
    }


    /**
     * 生成文档
     */
    public static void addTable(Document document, Map<String, String> table, List<Map<String, String>> columns) {
        try {
            // 构建表格说明
            String tableExplain = table.get("tableName") + " " + table.get("tableComment") + " ";
            Paragraph pheae = new Paragraph(tableExplain);
            document.add(pheae);

            // 创建表格
            Table tableElement = new Table(6);
            // 表格头部
            Color headColor = Color.decode("#DBE1F1");
            tableElement.setBorderWidth(1);
            tableElement.setPadding(0);
            tableElement.setSpacing(0);
            Cell cell = new Cell("字段名");
            cell.setBackgroundColor(headColor);
            cell.setHorizontalAlignment(1);
            cell.setHeader(true);
            tableElement.addCell(cell);
            cell = new Cell("数据类型");
            cell.setBackgroundColor(headColor);
            cell.setHorizontalAlignment(1);
            tableElement.addCell(cell);
            cell = new Cell("长度");
            cell.setBackgroundColor(headColor);
            cell.setHorizontalAlignment(1);
            tableElement.addCell(cell);
            cell = new Cell("可空");
            cell.setBackgroundColor(headColor);
            cell.setHorizontalAlignment(1);
            tableElement.addCell(cell);
            cell = new Cell("约束");
            cell.setBackgroundColor(headColor);
            cell.setHorizontalAlignment(1);
            tableElement.addCell(cell);
            cell = new Cell("字段描述");
            cell.setBackgroundColor(headColor);
            cell.setHorizontalAlignment(1);
            tableElement.addCell(cell);
            tableElement.endHeaders();

            // 表格主体，
            for (Map<String, String> column : columns) {
                //获取某表每个字段的详细说明
                tableElement.addCell(column.get("columnName"));
                tableElement.addCell(column.get("dataType"));
                tableElement.addCell(extractLength(column.getOrDefault("columnType", "")));
                tableElement.addCell(column.get("isNullable"));
                tableElement.addCell(column.get("columnKey"));
                tableElement.addCell(column.get("columnComment"));
            }

            //生成表格
            document.add(tableElement);

            // 换行
            document.add(new Paragraph());
        } catch (DocumentException e) {
            throw new RRException("生产word模板失败，表名：" + table.get("tableName"), e);
        }
    }

    private static Pattern pattern = Pattern.compile("(?<=\\()(.+?)(?=\\))");

    private static String extractLength(String origin) {
        Matcher matcher = pattern.matcher(origin);
        while (matcher.find()) {
            return matcher.group();
        }
        return "";
    }
}
